//
//  WKMnemonicsImportVM.m
//  WuKongUsernameLogin
//
//  Created by tt on 2023/9/16.
//

#import "WKMnemonicsImportVM.h"
#import "WKMnemnonInputCell.h"
#import <WuKongUsernameLogin/WuKongUsernameLogin-Swift.h>

@interface WKMnemonicsImportVM ()

@property(nonatomic,copy) NSString *mnemonics;

@end

@implementation WKMnemonicsImportVM

- (NSArray<NSDictionary *> *)tableSectionMaps {
    __weak typeof(self) weakSelf = self;
    return @[
        @{
            @"height":@(40.0f),
            @"items":@[
                    @{
                        @"class":WKMnemnonInputModel.class,
                        @"onChange":^(NSString* value){
                            weakSelf.mnemonics = value;
                        }
                    },
            ],
        },
        @{
            @"height":@(80.0f),
            @"items":@[
                    @{
                        @"class":WKButtonItemModel2.class,
                        @"title": LLang(@"确定导入"),
                        @"width": @(150.0f),
                        @"height":@(40.0f),
                        @"onPressed":^{
                            [weakSelf onNext];
                        }
                    },
            ],
        },
    
    ];
}

-(void) onNext {
    UIView *topView = WKNavigationManager.shared.topViewController.view;
    if(!self.mnemonics || [[self.mnemonics stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""]) {
        [topView showHUDWithHide:LLang(@"助记词不能为空！")];
        return;
    }
    NSString *username = WKApp.shared.loginInfo.extra[@"username"];
    NSString *uid = WKApp.shared.loginInfo.uid;
    NSString *privateKey = [WKWeb3Helper generatePrivatekey:self.mnemonics];
    if(!privateKey || [privateKey isEqualToString:@""]) {
        [topView showHUDWithHide:LLang(@"非法助记词，请检查")];
        return;
    }
    NSString *publickey = [WKWeb3Helper privateKeyToPublicKey:privateKey];
    NSLog(@"publickey---->%@",publickey);
    __weak typeof(self) weakSelf = self;
    [topView showHUD];
    
    [self getWeb3verifytext].then(^(NSDictionary *result){
        NSString *verifytext = result[@"verify_text"];
       NSData *verifyData = [verifytext dataUsingEncoding:NSUTF8StringEncoding];
        NSString *signText = [WKWeb3Helper signPersonalMessageWithPrivateKey:privateKey data:verifyData];
        [weakSelf web3verifySign:verifytext signText:signText username:username].then(^{
            [topView hideHud];
            WKApp.shared.loginInfo.extra[@"finishedWeb3Veritfy"] = @"1";
            [WKApp.shared.loginInfo save];
            [WKWeb3Helper createUserWallet:weakSelf.mnemonics uid:uid];
            [[WKApp shared] invoke:WKPOINT_LOGIN_SUCCESS param:nil];
        }).catch(^(NSError *error){
            [topView switchHUDError:error.domain];
        });
    }).catch(^(NSError*error){
        [topView switchHUDError:error.domain];
    });
}

-(AnyPromise*) getWeb3verifytext {
    NSString *username = WKApp.shared.loginInfo.extra[@"username"]?:@"";
    return [WKAPIClient.sharedClient GET:@"user/web3verifytext" parameters:@{@"username":username,@"type":@"login"}];
}

-(AnyPromise*) web3verifySign:(NSString*)verifyText signText:(NSString*)signText username:(NSString*)username {
   return [WKAPIClient.sharedClient POST:@"user/web3verifysign" parameters:@{
        @"verify_text": verifyText,
        @"sign_text": signText,
        @"username": username,
        @"type":@"login",
    }];
}

@end
